package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.mapper.ProjectInfoMapper;
import com.weixing.mall.provider.mapper.ProjectOperationMapper;
import com.weixing.mall.provider.model.domain.ProjectInfo;
import com.weixing.mall.provider.model.domain.ProjectOperation;
import com.weixing.mall.provider.model.dto.pdm.ProjectInfoDesignDto;
import com.weixing.mall.provider.model.dto.pdm.ProjectInfoDto;
import com.weixing.mall.provider.model.dto.pdm.ProjectInfoProductionDto;
import com.weixing.mall.provider.model.dto.pdm.ProjectInfoPurchaseDto;
import com.weixing.mall.provider.model.query.ProductDevelopQuery;
import com.weixing.mall.provider.model.vo.ProjectInfoVo;
import com.weixing.mall.provider.model.vo.pdm.ProjectInfoDesignVo;
import com.weixing.mall.provider.model.vo.pdm.ProjectInfoProductionVo;
import com.weixing.mall.provider.model.vo.pdm.ProjectInfoPurchaseVo;
import com.weixing.mall.provider.service.IProjectInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author wx
 * @Date 2019/9/10 11:50
 * @Version 1.0
 */
/**
 * <p>
 * 产品研发产品信息表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements IProjectInfoService {



    @Autowired
    private ProjectOperationMapper projectOperationMapper;

    @Override
    public IPage<ProjectInfoVo> selectListOfPage(Page page, ProductDevelopQuery productDevelopQuery) {
        return baseMapper.selectListOfPage(page,productDevelopQuery);
    }

    @Override
    public IPage<ProjectInfoDesignVo> selectDesignList(Page page, ProductDevelopQuery productDevelopQuery) {
        return baseMapper.selectDesignList(page,productDevelopQuery);
    }


    @Override
    public void saveDesign(ProjectInfoDesignDto projectInfoDesignDto, Long uid) {
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(projectInfoDesignDto,projectInfo);
        if(projectInfoDesignDto.getFabricTime()!=null){
            projectInfo.setFabricTime(projectInfoDesignDto.getFabricTime().getValue());
        }
        if(projectInfoDesignDto.getAccessoryTime()!=null){
            projectInfo.setAccessoryTime(projectInfoDesignDto.getAccessoryTime().getValue());
        }
        if(projectInfoDesignDto.getLiningMaterialTime()!=null){
            projectInfo.setLiningMaterialTime(projectInfoDesignDto.getLiningMaterialTime().getValue());
        }

        if(null== projectInfoDesignDto.getId()){
            save(projectInfo);
        }else {
            updateById(projectInfo);
        }

        //维护记录
        saveOperation(projectInfo.getProjectId(),"设计部维护",uid);
    }


    @Override
    public void save(ProjectInfoDto projectInfoDto,Long uid) {
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(projectInfoDto,projectInfo);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            projectInfo.setGermTemplateTime(format.parse(projectInfoDto.getGermTemplateTime()));
            projectInfo.setFirstTemplateTime(format.parse(projectInfoDto.getFirstTemplateTime()));
            projectInfo.setSecondTemplateTime(format.parse(projectInfoDto.getSecondTemplateTime()));
            projectInfo.setThirdTemplateTime(format.parse(projectInfoDto.getThirdTemplateTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

            updateById(projectInfo);
        //维护记录
        saveOperation(projectInfo.getProjectId(),"样板部维护",uid);
    }


    @Override
    public IPage<ProjectInfoPurchaseVo> selectPurchaseList(Page page, ProductDevelopQuery productDevelopQuery) {
        return baseMapper.selectPurchaseList(page,productDevelopQuery);
    }

    @Override
    public void savePurchase(ProjectInfoPurchaseDto projectInfoPurchaseDto, Long uid) {
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(projectInfoPurchaseDto,projectInfo);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            projectInfo.setRequirementTime(formatter.parse(projectInfoPurchaseDto.getRequirementTime()));
            projectInfo.setPurchaseContractTime(formatter.parse(projectInfoPurchaseDto.getPurchaseContractTime()));
            projectInfo.setPurchaseReturnTime(formatter.parse(projectInfoPurchaseDto.getPurchaseReturnTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        updateById(projectInfo);
        //维护记录
        saveOperation(projectInfo.getProjectId(),"采购部维护",uid);

    }


    @Override
    public IPage<ProjectInfoProductionVo> selectProductionList(Page page, ProductDevelopQuery productDevelopQuery) {
        return baseMapper.selectProductionList(page,productDevelopQuery);
    }

    @Override
    public void saveProduction(ProjectInfoProductionDto projectInfoProductionDto, Long uid) {
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(projectInfoProductionDto,projectInfo);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            projectInfo.setOrderTime(formatter.parse(projectInfoProductionDto.getOrderTime()));
            projectInfo.setCuttingTime(formatter.parse(projectInfoProductionDto.getCuttingTime()));
            projectInfo.setEstimateReturnTime(formatter.parse(projectInfoProductionDto.getEstimateReturnTime()));
            projectInfo.setActualReturnTime(formatter.parse(projectInfoProductionDto.getActualReturnTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
       updateById(projectInfo);
        //维护记录
        saveOperation(projectInfo.getProjectId(),"生产部维护",uid);
    }

    @Override
    public void delDesign(Long uid, Long id, Long projectId) {
        removeById(id);

        saveOperation(projectId,"设计部删除信息",uid);
    }

    private void saveOperation(Long projectId,String remark,Long uid){
        ProjectOperation projectOperation = new ProjectOperation();
        projectOperation.setCreatedBy(uid);
        projectOperation.setCreatedTime(new Date());
        projectOperation.setProductId(projectId);
        projectOperation.setRemark(remark);
        projectOperationMapper.insert(projectOperation);
    }
}
